<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>员工资料</el-breadcrumb-item>
      <el-breadcrumb-item>基本资料</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input v-model="queryInfo.query"
                    class="input-with-select"
                    :clearable="true"
                    placeholder="请输入内容"
                    @clear="getUserList"
                    @keyup.enter.native="getUserList">
            <el-button slot="append"
                       icon="el-icon-search"
                       @click="getUserList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary"
                     @click="addDialogVisible = true">添加用户</el-button>
        </el-col>
      </el-row>

      <!--  用户列表区域-->
      <el-table :data="tableData"
                :stripe="true"
                :border="true"
                style="width: 100%">
        <el-table-column type="index"
                         width="50">
        </el-table-column>
        <el-table-column prop="name"
                         label="姓名"
                         width="180">
        </el-table-column>
        <el-table-column prop="phone"
                         label="电话"
                         width="180">
        </el-table-column>
        <el-table-column prop="sex"
                         label="性别"
                         width="180">
        </el-table-column>
        <el-table-column prop="marital"
                         label="婚姻"
                         width="180">
        </el-table-column>
        <el-table-column prop="birthday"
                         label="生日">
        </el-table-column>
        <el-table-column prop="tags"
                         label="标签">
        </el-table-column>
        <el-table-column prop="notes"
                         label="备注">
        </el-table-column>
        <el-table-column fixed="right"
                         label="操作"
                         width="120">
          <template slot-scope="scope">
            <el-button @click="showEditDialog(scope.row.id)"
                       type="text"
                       size="small">编辑</el-button>
            <el-button @click="delconfirm(scope.row.id)"
                       type="text"
                       size="small">删除</el-button>
            <el-button @click="assign(scope.row)"
                       type="text"
                       size="small">分配</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页控件 -->
      <el-pagination @size-change="pagesizeChange"
                     @current-change="currentChange"
                     :current-page="queryInfo.pagenum"
                     :page-sizes="[1, 2, 5, 10]"
                     :page-size="queryInfo.pagesize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </el-card>

    <!-- 对话框区域 -->
    <!-- 新增员工对话框区域 -->
    <el-dialog title="新增员工"
               width="35%"
               :visible.sync="addDialogVisible">
      <el-form ref="addFormRef"
               :model="addform"
               label-width="70px">
        <el-form-item label="姓名"
                      prop="name">
          <el-input v-model="addform.name"
                    width="15px"></el-input>
        </el-form-item>
        <el-form-item label="电话"
                      prop="phone">
          <el-input v-model="addform.phone"></el-input>
        </el-form-item>
        <el-form-item label="性别"
                      prop="sex">
          <el-select v-model="addform.sex"
                     placeholder="请选择性别">
            <el-option label="男"
                       value="1"></el-option>
            <el-option label="女"
                       value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="婚况"
                      prop="marital">
          <el-select v-model="addform.marital"
                     placeholder="请选择婚况">
            <el-option label="已婚"
                       value="1"></el-option>
            <el-option label="未婚"
                       value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生日"
                      prop="birthday">
          <el-input v-model="addform.birthday"></el-input>
        </el-form-item>
        <el-form-item label="标签"
                      prop="tags">
          <el-input v-model="addform.tags"></el-input>
        </el-form-item>
        <el-form-item label="备注"
                      prop="notes">
          <el-input v-model="addform.notes"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="addEmp()">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑员工对话框区域 -->
    <el-dialog title="编辑员工"
               width="35%"
               :visible.sync="editDialogVisible">
      <el-form ref="editFormRef"
               :model="editform"
               label-width="70px">
        <el-form-item label="姓名"
                      prop="name">
          <el-input v-model="editform.name"
                    width="15px"
                    disabled></el-input>
        </el-form-item>
        <el-form-item label="电话"
                      prop="phone">
          <el-input v-model="editform.phone"></el-input>
        </el-form-item>
        <el-form-item label="性别"
                      prop="sex">
          <el-select v-model="editform.sex"
                     placeholder="请选择性别">
            <el-option label="男"
                       value="1"></el-option>
            <el-option label="女"
                       value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="婚况"
                      prop="marital">
          <el-select v-model="editform.marital"
                     placeholder="请选择婚况">
            <el-option label="已婚"
                       value="1"></el-option>
            <el-option label="未婚"
                       value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生日"
                      prop="birthday">
          <el-input v-model="editform.birthday"></el-input>
        </el-form-item>
        <el-form-item label="标签"
                      prop="tags">
          <el-input v-model="editform.tags"></el-input>
        </el-form-item>
        <el-form-item label="备注"
                      prop="notes">
          <el-input v-model="editform.notes"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer"
           class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="editEmp(editform.id)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Basic',
  created () {
    this.getUserList()
  },
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      tableData: [{}],
      addDialogVisible: false,
      editDialogVisible: false,
      addform: {
        name: '',
        phone: '',
        sex: '',
        marital: '',
        birthday: '',
        tags: '',
        notes: ''
      },
      editform: {
        id: '',
        name: '',
        phone: '',
        sex: '',
        marital: '',
        birthday: '',
        tags: '',
        notes: ''
      },
      total: ''
    }
  },
  methods: {
    async getUserList () {
      const { data: res } = await this.$http.get('/pageEmp', {
        params: this.queryInfo
      })
      if (res.code !== 200) {
        this.$message.error(res.message)
      }
      this.tableData = res.data.content
      this.total = res.data.pageInfo.total
    },
    async addEmp () {
      const { data: res } = await this.$http.post('/addEmp', this.addform)
      if (res.code !== 200) {
        this.$message.error(res.message)
      } else {
        // 先隐藏添加用户的对话框
        this.addDialogVisible = false
        // 再重新获取用户列表, 来刷新页面
        this.getUserList()
        // 最后提示添加成功
        this.$message.success('添加用户成功!')
      }
    },
    /**
     * 展示编辑用户的对话框
     * @param id
     */
    async showEditDialog (id) {
      // console.log(id)
      const { data: res } = await this.$http.get(`allEmp/${id}`)
      if (res.code !== 200) {
        //
        this.$message.error('查询用户信息失败!')
      }
      console.log(res)
      this.editform = res.data
      this.editDialogVisible = true
    },
    /**
     * 编辑用户信息提交
     * @param id
     */
    async editEmp (id) {
      const { data: res } = await this.$http.put(
        `updateEmp/${id}`,
        this.editform
      )
      if (res.code !== 200) {
        this.$message.error(res.message)
      } else {
        // 先隐藏添加用户的对话框
        this.editDialogVisible = false
        // 再重新获取用户列表, 来刷新页面
        this.getUserList()
        // 最后提示添加成功
        this.$message.success('修改用户成功!')
      }
    },
    /**
     * 删除确认提示框
     */
    async delconfirm (id) {
      const confirmResult = await this.$confirm(
        '是否永久删除用户, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => {
        return err
      })
      if (confirmResult !== 'confirm') {
        return this.$message.info('取消了删除!')
      } else {
        console.log('确认了删除!')
        const { data: res } = await this.$http.delete(`delEmp/${id}`)
        if (res.code !== 200) {
          return this.$message.error('删除用户失败!')
        } else {
          // 重新获取用户列表, 来刷新页面
          this.getUserList()
          return this.$message.success('删除用户成功!')
        }
      }
    },
    // 改变条数
    pagesizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getUserList()
    },
    // 改变页码
    currentChange (newCurrent) {
      this.queryInfo.pagenum = newCurrent
      this.getUserList()
    }
  }
}
</script>

<style lang="less" scoped>
</style>
